<?php
App::import('Model','User');
App::import('Model','Postcode');
class Locum extends User {
     var $useTable = 'users';    //don't use database
     var $validate = array();
     
     function ValidateData(){
        $this->validate = array(
                                "Username"=>array(
                                                    "rule1" => array(
                                                                    "rule" => "notEmpty",
                                                                    "message" => "Username is required",
                                                                    'last' => true
                                                                    ),
                                                    "rule2" => array(
                                                                    "rule" => array('minLength', 4),
                                                                    "message" => "Usernames must be at least 4 characters long",
                                                                    'last' => true
                                                                    ),
                                                    "rule3" => array(
                                                                    "rule" => array('maxLength', 50),
                                                                    "message" => "Usernames must be no larger than 50 characters long",
                                                                    'last' => true
                                                                    ),
                                                    "rule4" => array(
                                                                    "rule" =>'checkUsernameExisted',
                                                                    "message" => "Username already exists",
                                                                    'last' => true
                                                                    )
                                                ),
                                "FirstName" => array(
                                                    "rule1" => array(
                                                                        "rule" => "notEmpty",
                                                                        "message" => "FirstName is required",
                                                                        'last' => true
                                                                    ),
                                                  
                                                ),
                                "LastName" => array(
                                                    "rule1" => array(
                                                                        "rule" => "notEmpty",
                                                                        "message" => "FirstName is required",
                                                                        'last' => true
                                                                    )
                                                  
                                                ),
                                                 
                                "Address" => array(
                                                    "rule1" => array(
                                                                        "rule" => "notEmpty",
                                                                        "message" => "Address is required",
                                                                        'last' => true
                                                                    )
                                                  
                                                ),
                                                
                                "Postcode" => array(
                                                    "rule1" => array(
                                                                        "rule" => "notEmpty",
                                                                        "message" => "Postcode is required",
                                                                        'last' => true
                                                                    )
                                                ),
                                                
                                                
                                "Email" => array(
                                                    "rule1" => array(
                                                                        "rule" => "notEmpty",
                                                                        "message" => "Email is required",
                                                                        'last' => true
                                                                    ),
                                                    "rule2" => array(
                                                                        "rule" => "email",
                                                                        "message" => "Email not avaliables",
                                                                        'last' => true
                                                                    ),
                                                     "rule3" => array(
                                                                    "rule" =>'checkEmailExisted',
                                                                    "message" => "Email already exists",
                                                                    'last' => true
                                                                    )
                                                  
                                                ),
                                                
                                 "DateOfPharmacist" => array(
                                                    "rule1" => array(
                                                                        "rule" => "(((0[1-9]|[12][0-9]|3[01])([-./])(0[13578]|10|12)([-./])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-./])(0[469]|11)([-./])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-./])(02)([-./])(\d{4}))|((29)(\.|-|\/)(02)([-./])([02468][048]00))|((29)([-./])(02)([-./])([13579][26]00))|((29)([-./])(02)([-./])([0-9][0-9][0][48]))|((29)([-./])(02)([-./])([0-9][0-9][2468][048]))|((29)([-./])(02)([-./])([0-9][0-9][13579][26])))",
                                                                        "message" => "Date of pharmacist registration must like format dd/mm/yyyy",
                                                                        'allowEmpty' => true,
                                                                        'last' => true
                                                                        
                                                                    )
                                                  
                                                ),
                            );
                            
       if($this->validates($this->validate)) 
        return TRUE; 
       else 
        return FALSE;  
    } 
    
    // Check exists username in database 
    function checkUsernameExisted(){
        if($this->data['Locum']['Username']){ 
            // Check exists username in database
            if($this->data['Locum']['id'])
                $user = $this->find('first',array('conditions'=>array('Locum.Username'=>$this->data['Locum']['Username'],'Locum.UserType'=>1,'Locum.id <>' =>$this->data['Locum']['id'])));
            else
                $user = $this->find('first',array('conditions'=>array('Locum.Username'=>$this->data['Locum']['Username'],'Locum.UserType'=>1)));
           // print_r($user);die;
            if(!empty($user))
                return false; // Already exist
            else 
                return true; // Not in the database
        }
        else{
            return false;
        }
    }
    
    
    // Check exists email in database 
    function checkEmailExisted(){
        if($this->data['Locum']['Email']){ 
            // Check exists email in database
            if($this->data['Locum']['id'])
                $email = $this->find('first',array('conditions'=>array('Locum.Email'=>$this->data['Locum']['Email'],'Locum.UserType'=>1,'Locum.id <>' =>$this->data['Locum']['id'])));
            else
                $email = $this->find('first',array('conditions'=>array('Locum.Email'=>$this->data['Locum']['Email'],'Locum.UserType'=>1)));

            if(!empty($email))
                return false; // Already exist
            else 
                return true; // Not in the database
        }
        else{
            return true;
        }
    }
    
    function b_fCheckPostCode()
    {
        if($this->data['Locum']['Postcode'])
        {
           $obj_Postcode = new Postcode();
           return $obj_Postcode->b_fCheckPostCode($this->data['Locum']['Postcode']) ;
        }else
        {
            return false;
        }
    }
     
    function a_fGetPostCode($the_sz_PostCode)
    {
        $obj_Postcode = new Postcode();
        return $obj_Postcode->a_fGetPostCode($the_sz_PostCode) ;
    }
    
	function getLocumById($i_LocumId){
        
       return  $this->find('first',array( 'conditions'=>array(
                                                    'Locum.id'=>$i_LocumId,
                                                    ),
                                            )
        );  
    }
    
	function getAccountByUsernameAndType($sz_UserName, $sz_UserType){
        
       return  $this->find('first',array( 'conditions'=>array(
                                                    'Locum.Username'=>$sz_UserName,
       												'Locum.UserType'=>$sz_UserType
                                                    ),
                                            )
        );  
    }
}
?>